package server;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import link.ReceivedBuffer;

class InServer implements Runnable {
	ReceivedBuffer rb;
	int port;

	public InServer(ReceivedBuffer rb, int port) {
		this.rb = rb;
		this.port = port;
	}

	public InServer(ReceivedBuffer rb) {
		this.rb = rb;
		this.port = 11111;
	}

	public void run() {
		DatagramSocket s;
		byte[] buf;

		try {
			s = new DatagramSocket(port);

			while (true) {
				buf = new byte[16384];
				DatagramPacket pack = new DatagramPacket(buf, buf.length);
				s.receive(pack);
				// System.out.println("INFO: "+pack.getSocketAddress());

				String data = new String(pack.getData(), 0, pack.getLength());
				if (data != null) {
					rb.receiveBuffer.addFirst(data);

				}
			}
		} catch (IOException e) {
			System.out.println(e);
			System.exit(-1);
		}
	}
}
